<template>
  <div>
    <h2>影片库管理</h2>
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item>
        <el-button type="primary" @click="gotoCreate">创建新影片</el-button>
      </el-form-item>
      <el-form-item label="影片名称:">
        <el-input v-model="keyword" placeholder="请输入影片名称关键字查询"></el-input>
      </el-form-item>
      <el-form-item>
        <i class="el-icon-search" @click="doSearch()" style="font-size:150%;font-weight:500;"></i>
      </el-form-item>
    </el-form>
    <el-table :data="movieList" stripe style="width: 100%" v-loading="tableLoading">
      <el-table-column prop="movieId" label="影片Id" width="80">
      </el-table-column>
      <el-table-column prop="name" label="影片名称">
      </el-table-column>
      <el-table-column label="影片海报">
        <template slot-scope="scope">
          <img :src="scope.row.img" class="movie-img-tablecell">
        </template>
      </el-table-column>
      <el-table-column prop="rating" label="综合评分">
      </el-table-column>
      <el-table-column prop="personCount" label="参演人数">
      </el-table-column>
      <el-table-column prop="length" label="影片时长">
      </el-table-column>
      <el-table-column prop="moviePublishDate" label="上映日期">
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="100">
        <template slot-scope="scope">
          <el-button @click="rowEdit(scope.row)" type="text">编辑</el-button>
          <el-button type="text" @click="rowDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination class="page-tool" background layout="prev, pager, next" :total="total" :current-page="pageIndex+1"
      :page-size="pageSize" @current-change="pageChange">
    </el-pagination>
  </div>
</template>

<script>
export default {
  data () {
    return {
      movieList: [],
      total: 0,
      pageIndex: 0,
      pageSize: 5,
      keyword: '',
      tableLoading: false
    }
  },
  methods: {
    async loadMovieList () {
      this.tableLoading = true;
      let res = await this.$api.movie.getMovies(this.keyword, this.pageIndex, this.pageSize)
        .catch(error => {
          this.$message.error('获取影片列表出错：', error)
        }).finally(() => {
          this.tableLoading = false;
        })
      this.movieList = res.data.content;
      this.total = res.data.totalElements;
    },
    doSearch () {
      this.pageIndex = 0;
      this.loadMovieList();
    },
    pageChange (e) {
      console.log("分页切换", e);
      let page = e - 1;
      this.pageIndex = page;
      this.loadMovieList();
    },
    async rowDelete (movie) {
      console.log("删除影片按钮点击", movie);
      let confirmRes = await this.$confirm('影片[' + movie.name + ']将被删除, 是否继续?', '删除提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
      if (confirmRes) {
        let res = await this.$api.movie.deleteMovie(movie.movieId).catch(() => {
          this.$message.error('删除失败!');
        });
        if (res.status == 200) {
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
          this.loadMovieList()
        }
      }
    },
    rowEdit (movie) {
      console.log("编辑影片按钮点击", movie);
    },
    gotoCreate () {
      this.$router.push("/admin-el/create-movie")
    }
  },
  mounted () {
    this.loadMovieList()
  }
}
</script>

<style>
.movie-img-tablecell {
  width: 40px;
  height: 60px;
}

.page-tool {
  margin-top: 20px;
  text-align: right;
}
</style>
